<?php

namespace app\admin\service;

use app\admin\dao\PromotionEventDao;
use app\admin\dao\PromotionGroupFeedbackDao;
use app\admin\model\UM as UM;
use app\common\model\PromotionEvent;
use app\common\model\PromotionGroupFeedback;
use ShidianUtil\Exception\DataNotExistException;

/**
 * Class PromotionEventService
 * @package app\admin\service
 */
class PromotionEventService
{
    // dao
    private PromotionEventDao $promotionEventDao;

    private PromotionGroupFeedbackDao $feedbackDao;

    public function __construct(PromotionEventDao $promotionEventDao, PromotionGroupFeedbackDao $feedbackDao)
    {
        $this->promotionEventDao = $promotionEventDao;
        $this->feedbackDao = $feedbackDao;
    }

    /**
     * 新增
     * @param UM $user
     * @param array $params
     * @return  PromotionEvent
     */
    public function add(UM $user, array $params): PromotionEvent
    {
        $promotionGroupId = $params['promotionGroupId'];

        $promotionEvent = $this->promotionEventDao->insertActive($params, []);

        // 保存关联属性
        $this->saveEventFeedback($promotionGroupId, $params);

        return $promotionEvent;
    }

    /**
     * 更新
     * @param UM $user
     * @param int $id
     * @param array $params
     * @return PromotionEvent
     */
    public function modify(UM $user, int $id, array $params): PromotionEvent
    {
        $promotionGroupId = $params['promotionGroupId'];

        //这里如果要修改查询，建议把查询就放到这里
        $record = PromotionEvent::field(true)->where('id', $id)->find();

        if (is_null($record)) {
            throw new DataNotExistException('该事件不存在或已被删除');
        }

        $allowField = [];
        $this->promotionEventDao->updateActive($record, $params, $allowField);

        // 保存关联属性
        $this->saveEventFeedback($promotionGroupId, $params);

        return $record;
    }

    /**
     * 删除
     * @param UM $user
     * @param int $id
     */
    public function rm(UM $user, int $id): void
    {
        //这里如果要修改查询，建议把查询就放到这里
        $record = PromotionEvent::field(true)->where('id', $id)->find();

        if (is_null($record)) {
            return;
        }

        $this->promotionEventDao->deleteModel($record);

        // 一旦删除，删除关联数据
        PromotionGroupFeedback::destroy(fn($query) => $query->where('promotionEventId', $record->id));
    }

    private function saveEventFeedback($promotionGroupId, $params): void
    {
        $promotionEventId = $params['id'];

        // 清空旧数据
        PromotionGroupFeedback::destroy(function ($query) use ($promotionGroupId, $promotionEventId) {
            $query->where('promotionGroupId', $promotionGroupId)
                  ->where('promotionEventId', $promotionEventId);
        }, true);

        $actionIds = [];
        foreach ($params['actions'] as $action) {
            if ($action['isSel'] == 101) {
                $actionIds[] = $action['id'];
            }
        }
        $data = [
            'promotionGroupId' => $promotionGroupId,
            'promotionEventId' => $promotionEventId,
            'isSel' => $params['isSel'] ?? 102,
            'actionIds' => $actionIds,
            'deductionBase' => $params['deductionBase'],
            'deductionProportion' => $params['deductionProportion'],
        ];
        $this->feedbackDao->replaceActive($data);
    }
}
